package com.example.spark.controller;

import com.example.spark.entity.Top;
import com.example.spark.entity.result.CommonResult;
import com.example.spark.service.TopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@CrossOrigin
@RequestMapping("/top")
@RestController
public class TopController {
    @Autowired
    private TopService topService;

    /**
     * 添加头部
     *
     * @param top 头部
     * @return CommonResult
     */
    @PostMapping("/addTop")
    public CommonResult add(@RequestBody Top top) {
        return topService.add(top);
    }

    /**
     * 添加/修改头部图片
     *
     * @param file 图片文件
     * @param id 头部id
     * @return CommonResult
     * @throws IOException
     */
    @PutMapping("/updateTopPicture/{id}")
    public CommonResult addPicture(@RequestParam MultipartFile file,@PathVariable Long id) throws IOException {
        return topService.addPicture(file,id);
    }
    /**
     * 查询所有头部信息
     *
     * @return List<Top>
     */
    @GetMapping("/getTop")
    public CommonResult getAll() {
        return new CommonResult<>(200, "success",topService.getAll());
    }

    /**
     * 删除头部
     *
     * @param id 头部id
     * @return ComonResult
     */
    @DeleteMapping("/deleteTop/{id}")
    public CommonResult deleteById(@PathVariable Long id) {
        return topService.deleteById(id);
    }

    /**
     * 更新头部
     *
     * @param top 头部
     * @return CommonResult
     */
    @PutMapping("/updateTop")
    public CommonResult update(@RequestBody Top top) {
        return topService.update(top);
    }
}
